<?php
abstract class CV_OutputFilter_AbstractArrayDecorator extends CV_OutputFilter_RecursiveDecorator implements Countable,ArrayAccess,IteratorAggregate{
	public function toArray(){
		$escaped = array();
		foreach( $this->key_as($key) as $item ){
			if( is_object($key) ){
				$escaped[$key->toArrayKey()] = $item; // no decoration required here, since this is already done by the iterator
			} else {
				$escaped[$key] = $item;
			}
		}
		return $escaped;
	}
	
	public function offsetGet( $offset ){
		if( isset($this->decoratee[$offset]) ){
			return $this->output_filter->decorate( $this->decoratee[$offset] );
		} else {
			trigger_error( "Undefined index:  $offset", E_USER_NOTICE );
			return $this->output_filter->decorate(""); // <- dummy 
		}
	}

	public function getIterator(){
		$r = new ReflectionMethod( 'ArrayObject', 'getIterator' );
		return $this->output_filter->decorate( $r->invoke($this->decoratee) ); 
	}
	
	public function key_as( &$key ){
		$iterator = $this->getIterator();
		return $iterator->key_as( $key );
	}

	public function offsetSet( $offset, $value ){
		throw new CV_OutputFilter_Exception( 'This object is decorated with StringFilter and therefor read-only' );
	}
	public function offsetExists( $offset ){
		throw new CV_OutputFilter_Exception( 'This object is decorated with StringFilter and therefor isset is disabled' );
	}
	public function offsetUnset( $offset ){
		throw new CV_OutputFilter_Exception( 'This object is decorated with StringFilter and therefor read-only' );
	}
	public function count(){
		return $this->decoratee->count();
	}
	/*public function __toString(){
		trigger_error( "CV_OutputFilter error: Object of type '".get_class($this->decoratee)."' could not be converted to string"', E_USER_WARNING );
	}*/
}